package laf.socf.simulator.gui;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;

/**
 *
 * @author L.A. Fernando
 */
public class BrickWall extends Artifact implements MouseMotionListener,
        MouseListener, KeyListener {

    private int x,  y;
    private SimContext context = null;

    public SimContext getContext() {
        return context;
    }

    public void setContext(SimContext context) {
        this.context = context;
    }

    public BrickWall() {
    }

    public BrickWall(SimContext context) {
        this.setArtifactType(Artifact.TYPE_SOLID);
        this.context = context;
        this.setLocation(100, 100);
        this.setSize(15, 15);
        this.setIcon(new ImageIcon(getClass().getResource("/images/brick.png")));
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.addKeyListener(this);
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        int dx = (int) e.getPoint().getX() - this.x;
        int dy = (int) e.getPoint().getY() - this.y;
        int cx = (int) this.getLocation().getX();
        int cy = (int) this.getLocation().getY();
        int nx = cx + dx;
        int ny = cy + dy;
        if (nx < 0) {
            nx = 0;
        } else if (nx > context.getWidth() - this.getWidth()) {
            nx = context.getWidth() - this.getWidth();
        }
        if (ny < 0) {
            ny = 0;
        } else if (ny > context.getHeight() - this.getHeight()) {
            ny = context.getHeight() - this.getHeight();
        }
        this.setLocation(nx, ny);
    }

    public void mousePressed(MouseEvent e) {
        x = (int) e.getPoint().getX();
        y = (int) e.getPoint().getY();
        this.context.setBrickX(this.getX());
        this.context.setBrickY(this.getY());
        this.requestFocus();
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        BrickWall bw = null;
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            bw = new BrickWall(this.context);
            bw.setLocation(this.context.getBrickX() - this.getWidth(),
                    this.context.getBrickY());            
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            bw = new BrickWall(this.context);
            bw.setLocation(this.context.getBrickX() + this.getWidth(),
                    this.context.getBrickY());            
        } else if (e.getKeyCode() == KeyEvent.VK_UP) {
            bw = new BrickWall(this.context);
            bw.setLocation(this.context.getBrickX(),
                    this.context.getBrickY() - this.getHeight());            
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            bw = new BrickWall(this.context);
            bw.setLocation(this.context.getBrickX(),
                    this.context.getBrickY() + this.getHeight());            
        }
        if (bw != null) {
            this.context.addBrick(bw);
            this.context.setBrickX(bw.getX());
            this.context.setBrickY(bw.getY());
            this.context.repaint();
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }
}
